package com.face.pass.active;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Controller;

import com.face.pass.active.dao.UserDao;
import com.face.pass.active.model.User;
import com.face.pass.active.utils.ImageUtil;
import com.face.pass.active.view.LoginView;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;

import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import de.felixroske.jfxsupport.SplashScreen;
import javafx.scene.image.Image;
import javafx.stage.Stage;

/**
 * 人脸识别授权系统
 *
 */
@SpringBootApplication
@Controller
public class App extends AbstractJavaFxApplicationSupport {

	static final String LOADING_IMAGE = "/images/loading.gif";
	static final String ICON_IMAGE = "/images/icon.png";

	/**
	 * The entry point of application.
	 *
	 * @param args the input arguments
	 */
	public static void main(String[] args) {
		Locale.setDefault(Locale.CHINA);
		launch(App.class, LoginView.class, new SplashScreen() {
			@Override
			public String getImagePath() {
				return LOADING_IMAGE;
			}
		}, args);
	}

	/**
	 * Start.
	 * @param stage the stage
	 * @exception Exception the exception
	 */
	@Override
	public void start(Stage stage) throws Exception {
		super.start(stage);
	}

	@Override
	public void beforeShowingSplash(Stage splashStage) {
		splashStage.setTitle("人脸识别授权系统");
		splashStage.getIcons().add(ImageUtil.getImage(ICON_IMAGE));
		super.beforeShowingSplash(splashStage);
	}

	@Override
	public Collection<Image> loadDefaultIcons() {
		return Arrays.asList(new Image(getClass().getResource(ICON_IMAGE).toExternalForm()));
	}

	@Override
	public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
		UserDao userDao = ctx.getBean(UserDao.class);
		// 初始化代码可以在这里处理
		if (0 == userDao.count()) {
			User user = new User();
			user.setName("abc");
			String hashPassword = Hashing.md5().newHasher().putString("123", Charsets.UTF_8).hash().toString();
			user.setPassword(hashPassword);
			user.setCreateDate(new Date());
			userDao.saveAndFlush(user);
		}
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-2);
		
		//激活程序：1，七牛文件   格式：APPID@授权时间@授权量@剩余量
		
		
		super.beforeInitialView(stage, ctx);
	}

}
